package cz.fit.vutbr.pis2012.augeaswh.managers;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import cz.fit.vutbr.pis2012.augeaswh.persistence.Subscriber;

@Stateless
public class SubscriberManager {
	@PersistenceContext
	private EntityManager em;
	
	@SuppressWarnings("unchecked")
	public List<Subscriber> getSubscribers() {
		return em.createQuery("SELECT s FROM Subscriber s Where s.type = :type")
				.setParameter("type", "SUBSCRIBER").getResultList();
	}
	
	public void save(Subscriber subscriber) {
		if(subscriber.getBankAccount()!=null)
			em.merge(subscriber.getBankAccount());
		em.merge(subscriber);			
	}
	
	public void delete(Subscriber subscriber) {
	    em.remove(em.merge(subscriber));
	}
	
	public Subscriber getSupplierByID(Long id) {
		return (Subscriber) em.createQuery("SELECT s FROM Subject s Where s.type = :type AND s.id = :id")
									.setParameter("type", "SUBSCRIBER").setParameter("id", id).getSingleResult();
	}
}
